//Documentation complete
/**
 * @author Alex
 * @version 2009FE02
 */

package ui.internal.ajax;

import problemDomain.*;
import control.*;
import static problemDomain.ErrorBean.*;
import static utilities.XMLTools.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class MaintainMenu
 */
public class MaintainMenuItem extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = -829848742221197162L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml");
		HttpSession session = request.getSession(true);
		if (session != null) {
			String action = request.getParameter("a");
			if (action.equalsIgnoreCase("c"))
				create(request, response);
			else if (action.equalsIgnoreCase("r"))
				report(request, response);
			else if (action.equalsIgnoreCase("u"))
				update(request, response);
			else if (action.equalsIgnoreCase("d"))
				delete(request, response);
			else if (action.equalsIgnoreCase("l"))
				listAll(response);
			else if (action.equalsIgnoreCase("lf"))
				listFoodItem(response);
			else if (action.equalsIgnoreCase("fimi"))
				listFoodItemInMI(request, response);
			else if (action.equalsIgnoreCase("ai"))
				addItem(request, response);
			else if (action.equalsIgnoreCase("ri"))
				removeItem(request, response);
			else if (action.equalsIgnoreCase("si"))
				searchItem(request, response);
			else if (action.equalsIgnoreCase("m"))
				listMenu(response);
		}		
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String number = request.getParameter("number");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String menuID = request.getParameter("menuid");
		
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			
			MenuItemBean mi = miCtrl.create(menuID, name, price, number);
			out.write(getSuccess(Integer.toString(mi.getId())).toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String number = request.getParameter("number");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			List<MenuItemBean> list = miCtrl.search(id, number, name, price);
			out.write(encapsulateBeanListXML(list, "menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
		
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String number = request.getParameter("number");
		String menu = request.getParameter("menuid");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			miCtrl.update(id, menu, name, price, number);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			MenuItemControl menuCtrl = new MenuItemControl();
			menuCtrl.delete(id);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listAll (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			MenuItemControl menuCtrl = new MenuItemControl();
			List<MenuItemBean> list = menuCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listFoodItem (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			FoodItemControl fiCtrl = new FoodItemControl();
			List<FoodItemBean> list = fiCtrl.listAll();
			out.write(encapsulateBeanListXML(list,"fooditems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listFoodItemInMI (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			List<FoodItemBean> list = miCtrl.listFoodItems(id);
				System.out.println(list.size());
			out.write(encapsulateBeanListXML(list,"fooditems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void addItem (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String menuItemID = request.getParameter("menuitemid");
		String foodItemID = request.getParameter("fooditemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuItemControl menuCtrl = new MenuItemControl();
			menuCtrl.addFoodItem(menuItemID, foodItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void removeItem (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String menuItemID = request.getParameter("menuitemid");
		String foodItemID = request.getParameter("fooditemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuItemControl menuCtrl = new MenuItemControl();
			menuCtrl.removeFoodItem(menuItemID, foodItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void searchItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			FoodItemControl foodCtrl = new FoodItemControl();
			List<FoodItemBean> list = foodCtrl.search(name, type, null);
			out.write(encapsulateBeanListXML(list,"fooditems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listMenu(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuControl menuCtrl = new MenuControl();
			List<MenuBean> list = menuCtrl.listAll();
			out.write(encapsulateBeanListXML(list,"menus"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */
	
}
